package demo05_Prototype.model;

import java.io.*;

/**
 * 序列化的方式克隆
 */
public class CustomerDeepClone02 implements Serializable {


    private String name;
    private Integer age;
    private double height;
    private AddressDeepClone02 address;

    public CustomerDeepClone02() {
    }

    public CustomerDeepClone02(String name, Integer age, double height, AddressDeepClone02 address) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.address = address;
    }

    public AddressDeepClone02 getAddress() {
        return address;
    }

    public void setAddress(AddressDeepClone02 address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public Object clone()  {
        //将对象写入流中
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos= null;
        CustomerDeepClone02 clone = null;
        try {
            oos = new ObjectOutputStream(bao);
            oos.writeObject(this);
            //将对象从流中取出
            ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois=new ObjectInputStream(bis);
            clone = (CustomerDeepClone02)ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return clone;
    }

    @Override
    public String toString() {
        return "CustomerDeepClone02{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                ", address=" + address +
                '}';
    }
}
